import { addPickupAddressApi, modifyPickupAddressApi } from "@/api/client/configCenter/addressManagement";
import { getAreaListByCityApi, getCityListByProvinceApi, getStreetListByAreaApi } from "@/api/common";
import FormHook from "@/hook/formHook";
import { GeneraDataType } from "@/interface";
import { message } from "ant-design-vue";
export default function OperationHook(state: GeneraDataType) {
  const { formState, formDataRef, emit, type, passData, loading, streetOpts } = state;
  const { resetFields, validateIntergeHandle } = FormHook({ formState });
  const onFinish = () => {
    let requestFn;
    const params = {
      ...formState,
      customerId: passData.value.customerId ? passData.value.customerId : null
    };
    if (type.value === 1) {
      requestFn = addPickupAddressApi;
      Reflect.deleteProperty(params, "id");
    } else {
      requestFn = modifyPickupAddressApi;
      params.id = passData.value.id;
    }
    loading.value = true;
    requestFn(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      })
      .finally(() => (loading.value = false));
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  const loadData = (selectedOptions: any) => {
    const targetOption = selectedOptions[selectedOptions.length - 1];
    let requestFn: any;
    switch (targetOption.type) {
      case 1:
        requestFn = getCityListByProvinceApi;
        break;
      case 2:
        requestFn = getAreaListByCityApi;
        break;
      case 3:
        requestFn = getStreetListByAreaApi;
        break;
    }
    if (Object.prototype.toString.call(requestFn) === "[object Function]") {
      targetOption.loading = true;
      requestFn(targetOption.code)
        .then((res: any) => {
          const { code, data } = res;
          if (code === 200) {
            targetOption.child = data.map((t: any) => {
              return {
                code: t.code,
                name: t.name,
                type: t.type,
                isLeaf: t.type === 4
              };
            });
          }
        })
        .finally(() => (targetOption.loading = false));
    }
  };
  const changeCityModelHandle = (value: any, selectedOptions: Array<Base.GeneraDataType>) => {
    if (selectedOptions) {
      const state = selectedOptions[0];
      formState.state = state.name;
      formState.stateCode = state.code;
      const city = selectedOptions[1];
      formState.city = city.name;
      formState.cityCode = city.code;
      const zone = selectedOptions[2];
      formState.zone = zone.name;
      formState.zoneCode = zone.code;
      // const street = selectedOptions[3];
      formState.street = undefined;
      formState.streetCode = undefined;
      getStreetListByAreaApi(value[2]).then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          streetOpts.value = data || [];
        }
      });
    } else {
      formState.state = undefined;
      formState.stateCode = undefined;
      formState.city = undefined;
      formState.cityCode = undefined;
      formState.zone = undefined;
      formState.zoneCode = undefined;
      formState.street = undefined;
      formState.streetCode = undefined;
      streetOpts.value = [];
    }
  };
  const changeStreetHandle = () => {
    const { name } = streetOpts.value.find((t: any) => t.code === formState.streetCode);
    formState.street = name;
  };
  return {
    onFinish,
    onFinishFailed,
    resetHandle,
    resetFields,
    loadData,
    validateIntergeHandle,
    changeCityModelHandle,
    changeStreetHandle
  };
}
